#include "Bldc.h"
static void Bldc_UV(uint16_t CCR);
static void Bldc_WV(uint16_t CCR);
static void Bldc_WU(uint16_t CCR);
static void Bldc_VU(uint16_t CCR);
static void Bldc_VW(uint16_t CCR);
static void Bldc_UW(uint16_t CCR);
static uint8_t bldcGetHallAVal(void);
static uint8_t bldcGetHallBVal(void);
static uint8_t bldcGetHallCVal(void);

BldcRunState_t xBldcRunMode;//bldc运行状态

void BldcPwmControl(BldcPhase_t ePhase)
{
    uint16_t CCR = xMotorMode.CCR;
    switch(ePhase)
    {
        case PHASE1:
            Bldc_UV(CCR);
            break;
        case PHASE2:
            Bldc_WV(CCR);
            break;
        case PHASE3:
            Bldc_WU(CCR);
            break;            
        case PHASE4:
            Bldc_VU(CCR);
            break;            
        case PHASE5:
            Bldc_VW(CCR);
            break;
        case PHASE6:
            Bldc_UW(CCR);
            break;
        default:
            PWM_OFF();
            break;
    }
}

uint8_t BldcGetHallValue(void)
{
    UNION_U8 xHallVal = {0};
    xHallVal.BITS.B0 = bldcGetHallCVal();
    xHallVal.BITS.B1 = bldcGetHallBVal();    
    xHallVal.BITS.B2 = bldcGetHallAVal();

    return xHallVal.byte;
}

BldcPhase_t BldcHallValToPhase(uint8_t hallVal,uint16_t dir)
{
    BldcPhase_t ePhase = PHASE_ERROR;
    if( dir == 0 )
    {
        switch(hallVal)
        {
            case 3:
                ePhase = PHASE3;
                break;
            case 2:
                ePhase = PHASE4;
                break;
            case 6:
                ePhase = PHASE5;
                break;
            case 4:
                ePhase = PHASE6;
                break;
            case 5:
                ePhase = PHASE1;
                break;
            case 1:
                ePhase = PHASE2;
                break;
            default:
                ePhase = PHASE_ERROR;
                break;                                    
        }
    }
    else if( dir == 1 )
    {
        switch(hallVal)
        {
            case 3:
                ePhase = PHASE6;
                break;
            case 1:
                ePhase = PHASE5;
                break;
            case 5:
                ePhase = PHASE4;
                break;
            case 4:
                ePhase = PHASE3;
                break;
            case 6:
                ePhase = PHASE2;
                break;
            case 2:
                ePhase = PHASE1;
                break;
            default:
                ePhase = PHASE_ERROR;
                break;                                    
        }
    }

    return ePhase;
}

static void Bldc_UV(uint16_t CCR)
{
    PWM_CH1H_Ctrl(PWM_ON,CCR);//CH1上管PWM，下管利用互补机制取反
    PWM_CH2N_ON();//CH2下管高电平，上管强制低电平
    PWM_CH3_OFF();
}

static void Bldc_WV(uint16_t CCR)
{
    PWM_CH3H_Ctrl(PWM_ON,CCR);//CH3上管PWM，下管利用互补机制取反
    PWM_CH2N_ON();//CH2下管高电平，上管强制低电平
    PWM_CH1_OFF();  
}

static void Bldc_WU(uint16_t CCR)
{
    PWM_CH3H_Ctrl(PWM_ON,CCR);//CH3上管PWM，下管利用互补机制取反
    PWM_CH1N_ON();//CH1下管高电平，上管强制低电平
    PWM_CH2_OFF();
}

static void Bldc_VU(uint16_t CCR)
{
    PWM_CH2H_Ctrl(PWM_ON,CCR);//CH2上管PWM，下管利用互补机制取反
    PWM_CH1N_ON();//CH1下管高电平，上管强制低电平
    PWM_CH3_OFF();
}

static void Bldc_VW(uint16_t CCR)
{
    PWM_CH2H_Ctrl(PWM_ON,CCR);//CH2上管PWM，下管利用互补机制取反
    PWM_CH3N_ON();//CH3下管高电平，上管强制低电平
    PWM_CH1_OFF();    
}

static void Bldc_UW(uint16_t CCR)
{
    PWM_CH1H_Ctrl(PWM_ON,CCR);//CH1上管PWM，下管利用互补机制取反
    PWM_CH3N_ON();//CH3下管高电平，上管强制低电平
    PWM_CH2_OFF();    
}

static uint8_t bldcGetHallAVal(void)
{
    return HAL_GPIO_ReadPin(HA_GPIO_Port,HA_Pin);
}

static uint8_t bldcGetHallBVal(void)
{
    return HAL_GPIO_ReadPin(HB_GPIO_Port,HB_Pin);
}

static uint8_t bldcGetHallCVal(void)
{
    return HAL_GPIO_ReadPin(HC_GPIO_Port,HC_Pin);
}
